package com.stork.sms.activity;
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.navdrawer.SimpleSideDrawer;
import com.stork.sms.R;
import com.stork.sms.service.all.ServiceMethods;
import com.stork.sms.service.attendence.date.Pattandance;
import com.stork.sms.utilities.Constants;
import com.stork.sms.utilities.SharedPreference;

/**
 * @author admin
 *
 */
public class AttendanceLogActivity extends Activity {
	public static final String LOGIN_ID="username";
	private static final String SOAP_ACTION = "http://tempuri.org/getstudentattendance";

    private static final String METHOD_NAME = "getstudentattendance";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://10.0.2.2:2341/smsservice/webmethods.asmx";
    private SimpleSideDrawer ssdrawer;
    ProgressDialog progressdialog;
//    private Button buttonService;
    Pattandance pat;
    TextView dateval;
    final ArrayList<String> todoItems = new ArrayList<String>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_attendance_date);
		//dateval=(TextView)findViewById(R.id.vdate);
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("Attendance Log");
		actionBar.setDisplayHomeAsUpEnabled(true);
		ssdrawer=new SimpleSideDrawer(this);
		ssdrawer.setRightBehindContentView(R.layout.drawer);
		Toast.makeText(getApplicationContext(), "welcome",Toast.LENGTH_SHORT).show();
		new serviceCall().execute();
//		buttonService=(Button)findViewById(R.id.button_service);
//		buttonService.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//			Toast.makeText(getApplicationContext(), "welcome",Toast.LENGTH_SHORT).show();
//			new serviceCall().execute();
//			}
//		});
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	public boolean onOptionsItemSelected(MenuItem item) {
	      switch (item.getItemId()) {
	         // Respond to the action bar's Up/Home button
	         case android.R.id.home:
	        	 super.onBackPressed();
	         return true;
	         case R.id.action_settings:
	 			ssdrawer.toggleRightDrawer();
	 			return true;
	 		
	      }
	      return super.onOptionsItemSelected(item);
	   }
	class serviceCall extends AsyncTask<String, String,Pattandance>
	{
		Constants con;
		protected void onPostExecute(Pattandance result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(progressdialog.isShowing())
				progressdialog.dismiss();
			String code=pat.getCode();
			String msg=pat.getMessage();
			System.out.println("value code is"+code);
			System.out.println("value message is"+msg);
			System.out.println("fdate vaaaaaaaaaaaaaaaa"+pat.getAttendance().getFulldaylist().get(0).getDaylist());
	          System.out.println("fdate vaaaaaaaaaaaaaaaa"+pat.getAttendance().getHalfdaylist().get(0).getDaylist());
	          System.out.println("fdate vaaaaaaaaaaaaaaaa"+pat.getAttendance().getHalfdaylist().get(1).getDaylist());
	          for(int i=0;i<pat.getAttendance().getFulldaylist().size();i++){
			 todoItems.add(pat.getAttendance().getFulldaylist().get(i).getDaylist());
	          }
	          for(int j=0;j<pat.getAttendance().getHalfdaylist().size();j++){
	          todoItems.add(pat.getAttendance().getHalfdaylist().get(j).getDaylist()); 
	          
	          }
			if(code.equalsIgnoreCase(con.mSERVICEINVALID)){
//				Log.i("inside post","444 exception");
				
				AttendanceLogActivity.this.runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						 final AlertDialog.Builder alertBox = new AlertDialog.Builder(AttendanceLogActivity.this);
						// TODO Auto-generated method stub
						String msg=pat.getMessage();
						
						
						alertBox.setTitle("Error Message");
						alertBox.setMessage(msg);
						alertBox.setCancelable(true);
						alertBox.setPositiveButton("Yes",
			                    new DialogInterface.OnClickListener() {
			                public void onClick(DialogInterface dialog, int id) {
			                    Intent A=new Intent(AttendanceLogActivity.this,GridHomeMenuActivity.class);
			                    startActivity(A);
			                    finish();
			                    }
			                	
			            });
						AlertDialog ad=alertBox.create();
						ad.show();	
					}
					});
			
			}else if(code.equalsIgnoreCase(con.mSERVICEFAILED))
			{
				AttendanceLogActivity.this.runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						 final AlertDialog.Builder alertBox = new AlertDialog.Builder(AttendanceLogActivity.this);
						// TODO Auto-generated method stub
						String msg=pat.getMessage();
						
						
						alertBox.setTitle("Error Message");
						alertBox.setMessage(msg);
						alertBox.setCancelable(true);
						alertBox.setPositiveButton("Yes",
			                    new DialogInterface.OnClickListener() {
			                public void onClick(DialogInterface dialog, int id) {
			                    Intent A=new Intent(AttendanceLogActivity.this,GridHomeMenuActivity.class);
			                    startActivity(A);
			                    finish();
			                    }
			                	
			            });
						AlertDialog ad=alertBox.create();
						ad.show();	
					}
					});
			}
			else{
			System.out.println("Attendence log"+todoItems);
			 ListView lv=(ListView) findViewById(R.id.listView);
			
			ArrayAdapter<String> adap=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,todoItems);
			
		
			lv.setAdapter(adap);
			}
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progressdialog=new ProgressDialog(AttendanceLogActivity.this);
			progressdialog.setMessage("Please Wait......");
			progressdialog.setCancelable(false);
			progressdialog.show();
		}

		@Override
		protected Pattandance doInBackground(String... params) {
			Intent att=new Intent();
			String username=SharedPreference.readString(getApplicationContext(), LOGIN_ID,"");
			Bundle bundle=getIntent().getExtras();
			String fdate=bundle.getString("fdate");
			String tdate=bundle.getString("tdate");
			System.out.println("value for bundle date "+fdate);
			ServiceMethods sm=new ServiceMethods();
			pat=sm.PatAttendance(username, fdate, tdate);
			
			return pat;
		}
	}


	/*public void Call()
    {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        String w="DAV012K14LKG1";
        String f="2014/05/04";
        String t="2014/05/06";
   	 PropertyInfo properties=new PropertyInfo();
   	 PropertyInfo properties1=new PropertyInfo();
   	 PropertyInfo properties2=new PropertyInfo();
    	properties.name="studentid";
    	properties1.name="fromdate";
    	properties2.name="todate";
    	properties.type=PropertyInfo.STRING_CLASS;
    	
    	request.addProperty(properties,w);
    	request.addProperty(properties1,f);
    	request.addProperty(properties2,t);
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(URL);
        
        httpTransport.debug=true;
        
        try
        {
        	httpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive result=(SoapPrimitive) envelope.getResponse();
            Log.i("result", result.toString());
            
            System.out.println("resulttttttttttttttt"+result);
           String bracket =result.toString();
            GsonBuilder gsonBuilder=new GsonBuilder();
            Gson gson=gsonBuilder.create();
          pat=gson.fromJson(bracket, Pattandance.class);
          
          System.out.println("fdate vaaaaaaaaaaaaaaaa"+pat.getAttendance().getFulldaylist().get(0).getDaylist());
          System.out.println("fdate vaaaaaaaaaaaaaaaa"+pat.getAttendance().getHalfdaylist().get(0).getDaylist());
          System.out.println("fdate vaaaaaaaaaaaaaaaa"+pat.getAttendance().getHalfdaylist().get(1).getDaylist());
          
          todoItems.add(pat.getAttendance().getFulldaylist().get(0).getDaylist());
          todoItems.add(pat.getAttendance().getHalfdaylist().get(0).getDaylist()); 
          todoItems.add(pat.getAttendance().getHalfdaylist().get(1).getDaylist()); 
        }
        catch (SocketException exception)
        {
            System.out.println(exception);
        } catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
    }*/
}
	

